﻿/*
  In App.xaml:
  <Application.Resources>
      <vm:ViewModelLocatorTemplate xmlns:vm="clr-namespace:DrinkLocatorMvvm.ViewModel"
                                   x:Key="Locator" />
  </Application.Resources>
  
  In the View:
  DataContext="{Binding Source={StaticResource Locator}, Path=ViewModelName}"
  
  OR (WPF only):
  
  xmlns:vm="clr-namespace:DrinkLocatorMvvm.ViewModel"
  DataContext="{Binding Source={x:Static vm:ViewModelLocatorTemplate.ViewModelNameStatic}}"
*/
using Locator.Silverlight.Client.Models.ViewModels.Screens;

namespace Locator.Silverlight.Client.Models.ViewModels
{
    /// <summary>
    /// This class contains static references to all the view models in the
    /// application and provides an entry point for the bindings.
    /// <para>
    /// Use the <strong>mvvmlocatorproperty</strong> snippet to add ViewModels
    /// to this locator.
    /// </para>
    /// <para>
    /// In Silverlight and WPF, place the ViewModelLocatorTemplate in the App.xaml resources:
    /// </para>
    /// <code>
    /// &lt;Application.Resources&gt;
    ///     &lt;vm:ViewModelLocatorTemplate xmlns:vm="clr-namespace:DrinkLocatorMvvm.ViewModel"
    ///                                  x:Key="Locator" /&gt;
    /// &lt;/Application.Resources&gt;
    /// </code>
    /// <para>
    /// Then use:
    /// </para>
    /// <code>
    /// DataContext="{Binding Source={StaticResource Locator}, Path=ViewModelName}"
    /// </code>
    /// <para>
    /// You can also use Blend to do all this with the tool's support.
    /// </para>
    /// <para>
    /// See http://www.galasoft.ch/mvvm/getstarted
    /// </para>
    /// <para>
    /// In <strong>*WPF only*</strong> (and if databinding in Blend is not relevant), you can delete
    /// the Main property and bind to the ViewModelNameStatic property instead:
    /// </para>
    /// <code>
    /// xmlns:vm="clr-namespace:DrinkLocatorMvvm.ViewModel"
    /// DataContext="{Binding Source={x:Static vm:ViewModelLocatorTemplate.ViewModelNameStatic}}"
    /// </code>
    /// </summary>
    public class ViewModelLocator
    {
        private static ManageProductsViewModel manageProductsviewModel;

        /// <summary>
        /// Gets the ManageProductsViewModel property.
        /// </summary>
        public static ManageProductsViewModel ManageProductsViewModelStatic
        {
            get
            {
                if (manageProductsviewModel == null)
                {
                    CreateManageProductsViewModel();
                }

                return manageProductsviewModel;
            }
        }

        /// <summary>
        /// Gets the ManageProductsViewModel property.
        /// </summary>
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance",
            "CA1822:MarkMembersAsStatic",
            Justification = "This non-static member is needed for data binding purposes.")]
        public ManageProductsViewModel ManageProductsViewModel
        {
            get { return ManageProductsViewModelStatic; }
        }

        /// <summary>
        /// Provides a deterministic way to delete the ManageProductsViewModel property.
        /// </summary>
        public static void ClearManageProductsViewModel()
        {
            if (manageProductsviewModel != null)
            {
                manageProductsviewModel.Cleanup();
            }

            manageProductsviewModel = null;
        }

        /// <summary>
        /// Provides a deterministic way to create the ManageProductsViewModel property.
        /// </summary>
        public static void CreateManageProductsViewModel()
        {
            if (manageProductsviewModel == null)
            {
                manageProductsviewModel = new ManageProductsViewModel();
            }
        }

        private static MainViewModel main;
        private static ResultDetailsViewModel resultDetails;

        /// <summary>
        /// Initializes a new instance of the ViewModelLocator class.
        /// </summary>
        public ViewModelLocator()
        {
            ////if (ViewModelBase.IsInDesignModeStatic)
            ////{
            ////    // Create design time view models
            ////}
            ////else
            ////{
            ////    // Create run time view models
            ////}

            CreateMain();
            CreateResultDetails();
            //CreateManageProductsViewModel();
        }

        /// <summary>
        /// Gets the Main property.
        /// </summary>
        public static MainViewModel MainStatic
        {
            get
            {
                if (ViewModelLocator.main == null)
                {
                    CreateMain();
                }

                return ViewModelLocator.main;
            }
        }

        public static ResultDetailsViewModel ResultDetailsStatic
        {
            get
            {
                if (resultDetails == null)
                {
                    CreateResultDetails();
                }

                return resultDetails;
            }
        }

        private static void CreateResultDetails()
        {
            if (resultDetails == null)
            {
                resultDetails = new ResultDetailsViewModel();
            }
        }

        /// <summary>
        /// Gets the Main property.
        /// </summary>
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance",
            "CA1822:MarkMembersAsStatic",
            Justification = "This non-static member is needed for data binding purposes.")]
        public MainViewModel Main
        {
            get { return ViewModelLocator.MainStatic; }
        }

         [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance",
            "CA1822:MarkMembersAsStatic",
            Justification = "This non-static member is needed for data binding purposes.")]
        public ResultDetailsViewModel ResultDetails
        {
            get { return ResultDetailsStatic; }
        }

        /// <summary>
        /// Provides a deterministic way to delete the Main property.
        /// </summary>
        public static void ClearMain()
        {
            ViewModelLocator.main.Cleanup();
            ViewModelLocator.main = null;
        }

        /// <summary>
        /// Provides a deterministic way to delete the Main property.
        /// </summary>
        public static void ClearResultDetails()
        {
            resultDetails.Cleanup();
            resultDetails = null;
        }

        /// <summary>
        /// Provides a deterministic way to create the Main property.
        /// </summary>
        public static void CreateMain()
        {
            if (ViewModelLocator.main == null)
            {
                ViewModelLocator.main = new MainViewModel();
            }
        }

        /// <summary>
        /// Cleans up all the resources.
        /// </summary>
        public static void Cleanup()
        {
            ClearMain();
            ClearResultDetails();
            ClearManageProductsViewModel();
            ClearManageStoresViewModel();
            ClearManagePersonalLocations();
        }

        private static ManageStoresViewModel manageStores;

        /// <summary>
        /// Gets the ManageStoresViewModel property.
        /// </summary>
        public static ManageStoresViewModel ManageStoresStatic
        {
            get
            {
                if (manageStores == null)
                {
                    CreateManageStoresViewModel();
                }

                return manageStores;
            }
        }

        /// <summary>
        /// Gets the ManageStoresViewModel property.
        /// </summary>
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance",
            "CA1822:MarkMembersAsStatic",
            Justification = "This non-static member is needed for data binding purposes.")]
        public ManageStoresViewModel ManageStores
        {
            get
            {
                return ManageStoresStatic;
            }
        }

        /// <summary>
        /// Provides a deterministic way to delete the ManageStoresViewModel property.
        /// </summary>
        public static void ClearManageStoresViewModel()
        {
            if (manageStores != null)
            {
                manageStores.Cleanup();
            }

            manageStores = null;
        }

        /// <summary>
        /// Provides a deterministic way to create the ManageStoresViewModel property.
        /// </summary>
        public static void CreateManageStoresViewModel()
        {
            if (manageStores == null)
            {
                manageStores = new ManageStoresViewModel();
            }
        }

        private static ManagePersonalLocationsViewModel managePersonalLocations;

        /// <summary>
        /// Gets the ManagePersonalLocations property.
        /// </summary>
        public static ManagePersonalLocationsViewModel ManagePersonalLocationsStatic
        {
            get
            {
                if (managePersonalLocations == null)
                {
                    CreateManagePersonalLocations();
                }

                return managePersonalLocations;
            }
        }

        /// <summary>
        /// Gets the ManagePersonalLocations property.
        /// </summary>
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance",
            "CA1822:MarkMembersAsStatic",
            Justification = "This non-static member is needed for data binding purposes.")]
        public ManagePersonalLocationsViewModel ManagePersonalLocations
        {
            get
            {
                return ManagePersonalLocationsStatic;
            }
        }

        /// <summary>
        /// Provides a deterministic way to delete the ManagePersonalLocations property.
        /// </summary>
        public static void ClearManagePersonalLocations()
        {
            if (managePersonalLocations != null)
            {
                managePersonalLocations.Cleanup();
            }

            managePersonalLocations = null;
        }

        /// <summary>
        /// Provides a deterministic way to create the ManagePersonalLocations property.
        /// </summary>
        public static void CreateManagePersonalLocations()
        {
            if (managePersonalLocations == null)
            {
                managePersonalLocations = new ManagePersonalLocationsViewModel();
            }
        }
    }
}